package com.hy.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hy.Result.ResponseResult;
import com.hy.mapper.VaccineCategoryMapper;
import com.hy.pojo.vaccine.Vaccine;
import com.hy.pojo.vaccine.VaccineCategory;
import com.hy.pojeVo.QueryParam;
import com.hy.service.VaccineCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 疫苗分类服务实现类
 */
@Service
public class VaccineCategoryServiceImpl extends ServiceImpl<VaccineCategoryMapper, VaccineCategory>
    implements VaccineCategoryService {

    @Autowired
    private VaccineCategoryMapper vaccineCategoryMapper;

    @Override
    public List<VaccineCategory> findByName() {
        return vaccineCategoryMapper.findByName();
    }

    @Override
    public List<VaccineCategory> findName() {
        return vaccineCategoryMapper.findName();
    }

    @Override
    public ResponseResult findVaccinesByCategoryId(Long categoryId) {
        try {
            List<Vaccine> vaccines = vaccineCategoryMapper.findVaccinesByCategoryId(categoryId);
            if (vaccines != null && !vaccines.isEmpty()) {
                return new ResponseResult(200, "查询成功", vaccines);
            }
            String message = categoryId == null ? "暂无疫苗数据" : "该分类下暂无疫苗";
            return new ResponseResult(200, message, null);
        } catch (Exception e) {
            return new ResponseResult(500, "查询失败：" + e.getMessage(), null);
        }
    }

    @Override
    public ResponseResult searchByKeyword(QueryParam queryParam) {
        try {
            PageHelper.startPage(queryParam.getPageNum(), queryParam.getPageSize());
            LambdaQueryWrapper<VaccineCategory> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(VaccineCategory::getIsDeleted, 0)
                  .eq(VaccineCategory::getStatus, 1)
                  .orderByAsc(VaccineCategory::getSortOrder);
            
            List<VaccineCategory> categories = vaccineCategoryMapper.selectList(wrapper);
            PageInfo<VaccineCategory> pageInfo = new PageInfo<>(categories);
            return ResponseResult.okResult(200, "查询成功!", pageInfo);
        } catch (Exception e) {
            return new ResponseResult(500, "查询失败：" + e.getMessage(), null);
        }
    }
}




